using Assets.Scripts.GDK;
using UnityEngine;
using UnityEngine.UI;

namespace Brick
{
	public class chooseitem : MonoBehaviour
	{
		public Item item = new Item();

		public Image image;

		public Sprite defaultsp;

		public Sprite[] sp;

		public Sprite[] daojusp;

		public Sprite[] colorsp;

		public InputField intputField;

		private int colorType;

		private Sprite curSp;

		public Text openTip;

		private bool defaultOpen = true;

		private void Start()
		{
			gdk_EventManager.Instance.AddEventListener<Item>("CHOOSE", OnClick1);
			gdk_EventManager.Instance.AddEventListener<Item>("CHOOSETYPE", OnClick2);
			gdk_EventManager.Instance.AddEventListener<Item>("CHOOSECOLOR", OnClick3);
		}

		private void Update()
		{
		}

		public Item GetItem()
		{
			return item;
		}

		public Sprite getCurSP()
		{
			return curSp;
		}

		public void OnClick1(Item curItem)
		{
			item.type = curItem.type;
			item.ID = curItem.ID;
			item.num = 0;
			Sprite sprite = curSp = getSprite(item.ID, item.type);
			image.sprite = sprite;
			if (intputField.text != string.Empty)
			{
				item.num = int.Parse(intputField.text);
			}
		}

		public void OnClick2(Item curItem)
		{
			item.type = curItem.type;
			item.ID = curItem.ID;
			item.num = 0;
			colorType = item.ID;
		}

		public void OnClick3(Item curItem)
		{
			item.type = curItem.type;
			item.ID = curItem.ID;
			item.num = 0;
			Sprite sprite = curSp = getColorSpirte(item.ID, colorType);
			image.sprite = sprite;
			if (intputField.text != string.Empty)
			{
				item.num = int.Parse(intputField.text);
			}
			item.ID = 5 * item.ID + colorType;
		}

		public Sprite getColorSpirte(int iColor, int iColorType)
		{
			string id = "UI/Brick/tq_brick_3_" + iColor + "_" + iColorType;
			Sprite sprite = gdk_game_manager.loadSprite(id);
			if ((bool)sprite)
			{
				return sprite;
			}
			return defaultsp;
		}

		public Sprite getSprite(int cID, int ctype)
		{
			switch (ctype)
			{
				case 1:
					return sp[cID];
				case 2:
					return daojusp[cID];
				case 3:
					{
						int iColor = cID / 5;
						int iColorType = cID % 5;
						return getColorSpirte(iColor, iColorType);
					}
				default:
					return defaultsp;
			}
		}

		public void OnChange()
		{
			if (intputField.text != string.Empty)
			{
				item.num = int.Parse(intputField.text);
			}
		}

		public void OnOpenClick()
		{
			defaultOpen = !defaultOpen;
			if (defaultOpen)
			{
				openTip.text = "True";
			}
			else
			{
				openTip.text = "False";
			}
		}

		public bool getOpen()
		{
			return defaultOpen;
		}
	}
}

